﻿using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;

namespace AnyOffice.general.rms.roll_manage
{
    public partial class _new : ModulePage
    {
        protected string strTemp;

        private void BindDept(ref DropDownList ddlParentDept, int DeptId, string strTemp)
        {
            DataTable table = new DepartmentDao().SelectChildAll(base.Conn, DeptId);
            if (table.Rows.Count > 0)
            {
                strTemp = strTemp + "|　";
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    ListItem item = new ListItem(strTemp + "|─" + table.Rows[i]["DeptName"].ToString(), table.Rows[i]["DeptId"].ToString());
                    ddlParentDept.Items.Add(item);
                    this.BindDept(ref ddlParentDept, int.Parse(table.Rows[i]["DeptId"].ToString()), strTemp);
                }
            }
        }
 

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                DataTable table = new RmsRoomDao().SelectAll(base.Conn);
                this.ddlRoomId.DataSource = table;
                this.ddlRoomId.DataBind();
                this.ddlRoomId.Items.Insert(0, new ListItem("", ""));
                ListItem item = new ListItem("", "");
                this.ddlDeptId.Items.Add(item);
                DataTable table2 = new DepartmentDao().SelectParentAll(base.Conn);
                if (table2.Rows.Count > 0)
                {
                    for (int i = 0; i < table2.Rows.Count; i++)
                    {
                        item = new ListItem("|─" + table2.Rows[i]["DeptName"].ToString(), table2.Rows[i]["DeptId"].ToString());
                        this.ddlDeptId.Items.Add(item);
                        this.strTemp = "";
                        this.BindDept(ref this.ddlDeptId, int.Parse(table2.Rows[i]["DeptId"].ToString()), this.strTemp);
                    }
                }
                this.ddlSecret.DataSource = new RmsCodeDao().SelectAll(base.Conn, RmsCodeDao.CodeType.Secret);
                this.ddlSecret.DataBind();
                this.ddlSecret.Items.Insert(0, new ListItem("", ""));
                this.ddlCertificateKind.DataSource = new RmsCodeDao().SelectAll(base.Conn, RmsCodeDao.CodeType.CertificateKind);
                this.ddlCertificateKind.DataBind();
                this.ddlCertificateKind.Items.Insert(0, new ListItem("", ""));
            }
            if (this.Page.IsPostBack)
            {
                RmsRollData data = new RmsRollData();
                data.RollCode = this.txtRollCode.Text;
                data.RollName = this.txtRollName.Text;
                data.RoomId = Convert.ToInt32(this.ddlRoomId.SelectedValue);
                data.Years = this.txtYears.Text;
                if (this.txtBeginDate.Value != string.Empty)
                {
                    data.BeginDate = Convert.ToDateTime(this.txtBeginDate.Value);
                }
                if (this.txtEndDate.Value != string.Empty)
                {
                    data.EndDate = Convert.ToDateTime(this.txtEndDate.Value);
                }
                if (this.ddlDeptId.SelectedValue != string.Empty)
                {
                    data.DeptId = Convert.ToInt32(this.ddlDeptId.SelectedValue);
                }
                if (this.txtBZJG.Text != string.Empty)
                {
                    data.BZJG = this.txtBZJG.Text;
                }
                if (this.txtDeadLine.Text != string.Empty)
                {
                    data.DeadLine = this.txtDeadLine.Text;
                }
                if (this.ddlSecret.SelectedValue != string.Empty)
                {
                    data.Secret = this.ddlSecret.SelectedValue;
                }
                if (this.txtCategoryNo.Text != string.Empty)
                {
                    data.CategoryNo = this.txtCategoryNo.Text;
                }
                if (this.txtCatalogNo.Text != string.Empty)
                {
                    data.CatalogNo = this.txtCatalogNo.Text;
                }
                if (this.txtArchiveNo.Text != string.Empty)
                {
                    data.ArchiveNo = this.txtArchiveNo.Text;
                }
                if (this.txtBoxNo.Text != string.Empty)
                {
                    data.BoxNo = this.txtBoxNo.Text;
                }
                if (this.txtMicroNo.Text != string.Empty)
                {
                    data.MicroNo = this.txtMicroNo.Text;
                }
                if (this.ddlCertificateKind.SelectedValue != string.Empty)
                {
                    data.CertificateKind = this.ddlCertificateKind.SelectedValue;
                }
                if (this.txtCertificateStart.Text != string.Empty)
                {
                    data.CertificateStart = this.txtCertificateStart.Text;
                }
                if (this.txtCertificateEnd.Text != string.Empty)
                {
                    data.CertificateEnd = this.txtCertificateEnd.Text;
                }
                if (this.txtRollPage.Text != string.Empty)
                {
                    data.RollPage = this.txtRollPage.Text;
                }
                if (this.txtRemark.Text != string.Empty)
                {
                    data.Remark = this.txtRemark.Text;
                }
                data.AddUser = base.User.Identity.Name;
                data.AddTime = DateTime.Now;
                new RmsRollDao().Insert(base.Conn, data);
                this.pnlMain.Visible = false;
                this.litMessage.Text = base.ShowPrompt("新建案卷成功！", "new.aspx");
            }


        }
    }
}
